import time
import datetime

from gevent import os
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


from PoProject.outputs.Logs.log import get_log
from PoProject.getcwd import get_cwd


class basePage(object):
    def __init__(self, driver=None):
        self.driver = driver
        self.log = get_log('Po_project')

    # 等待元素可见
    def __wait_element_visible(self, locator, times=10, poll_frequency=0.5, path=''):
        """
        :param locator: 元素定位
        :param times: 等待最长时间
        :param poll_frequency: 查询间隔
        :param path: 等待元素的路径 百度首页_搜索功能_搜索按钮
        :return:
        """
        try:
            start_time = datetime.datetime.now()
            WebDriverWait(self.driver, times, poll_frequency).until(EC.visibility_of_element_located(locator))
            end_time = datetime.datetime.now()
            self.log.info("{}等待元素{} ,耗时{}秒".format(path, locator, (end_time-start_time).seconds))
        except:
            self.log.exception("{}等待元素{}不可见".format(path, locator))
            self.__get_ScreenShot(path)

    # 等待元素存在
    def wait_element_presence(self, locator, times=10, poll_frequency=0.5, path=''):
        self.log.info("{} 等待元素 {} 存在".format(path, locator))
        try:
            startTime = datetime.datetime.now()
            WebDriverWait(self.driver, times, poll_frequency).until(EC.presence_of_element_located(locator))
            endTime = datetime.datetime.now()
            self.log.info("{} 元素 {}等待结束，等待时长为：{} 秒".format(path, locator, (endTime - startTime).seconds))
            return True
        except:
            self.log.exception("{} 等待元素 {} 存在失败".format(path, locator))
            self.__get_ScreenShot(path)
            return False

    # 查找元素
    def __get_element(self, locator, path=''):
        self.__wait_element_visible(locator=locator, path=path)
        try:
            ele = self.driver.find_element(*locator)
            self.log.info("{} 元素{}查找成功".format(path, locator))
            return ele
        except:
            self.log.exception("{} 元素{}查找失败".format(path, locator))
            self.__get_ScreenShot(path)

    # 点击元素
    def click_element(self, locator, path=''):
        # 第一步 查找元素
        el = self.__get_element(locator=locator, path= path)
        try:
            el.click()
            self.log.info("{} 元素{}点击成功".format(path, locator))
        except:
            self.log.exception("{} 元素{}点击失败".format(path, locator))
            self.__get_ScreenShot(path)

    # 查找元素并输入
    def input_text(self, locator, text, path=''):
        # 第一步 查找元素
        el = self.__get_element(locator=locator, path=path)
        try:
            el.send_keys(text)
            self.log.info("{} 元素{}输入成功".format(path, locator))
        except:
            self.log.exception("{} 元素{}输入失败".format(path, locator))
            self.__get_ScreenShot(path)

    # 获取浏览器title
    def get_browserTitle(self, path=''):
        try:
            return self.driver.title
        except:
            self.log.exception("{} 标题获取失败".format(path))
            self.__get_ScreenShot(path)

    # 截图
    def __get_ScreenShot(self, path1=''):
        dirPath = os.path.join(get_cwd(), 'outputs/picture/')
        name = dirPath+path1+time.strftime('%Y_%h_%m_%H_%M_%S')+'.png'
        try:
            self.driver.get_screenshot_as_file(name)
        except:
            self.log.exception("{}截图失败".format(path1))
